#ifndef AISERVICESETTINGS_H
#define AISERVICESETTINGS_H

#include <QString>
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <utility>

#include "llamaServerManager.h"


#define DEFAULT_CHAT_AI_URL "http://localhost:11434/v1/chat/completions"
#define DEFAULT_CHAT_AI_KEY "aaa"
#define DEFAULT_CHAT_AI_MODEL "minicpm-v:latest"
#define DEFAULT_EMBEDDING_AI_URL "http://localhost:11434/v1/embeddings"
#define DEFAULT_EMBEDDING_AI_KEY "aaa"
#define DEFAULT_EMBEDDING_AI_MODEL "bge-m3:latest"

#define DEFAULT_USE_BUILTIN_CHAT_AI false
#define DEFAULT_USE_BUILTIN_EMBEDDING_AI false
#define DEFAULT_CHAT_AI_MODEL_PATH "/models/minicpm-v.gguf"
#define DEFAULT_CHAT_AI_HOST "0.0.0.0"
#define DEFAULT_CHAT_AI_PORT "11434"
#define DEFAULT_CHAT_AI_TEMP "0"
#define DEFAULT_CHAT_AI_CONTEXT_SIZE "0"
#define DEFAULT_CHAT_AI_NGL "100"
#define DEFAULT_EMBEDDING_AI_MODEL_PATH "/models/bge-m3.gguf"
#define DEFAULT_EMBEDDING_AI_HOST "0.0.0.0"
#define DEFAULT_EMBEDDING_AI_PORT "11434"
#define DEFAULT_EMBEDDING_AI_TEMP "0"
#define DEFAULT_EMBEDDING_AI_CONTEXT_SIZE "0"
#define DEFAULT_EMBEDDING_AI_NGL "100"

class AIServiceSettings : public QObject
{
    Q_OBJECT

public:
    explicit AIServiceSettings(QObject *parent = nullptr);

    void loadSettings();
    void saveSettings();
    void openSettingsDialog();

    QString getChatAIUrl() const { return chatAIUrl; }
    QString getChatAIKey() const { return chatAIKey; }
    QString getChatAIModel() const { return chatAIModel; }
    QString getEmbeddingAIUrl() const { return embeddingAIUrl; }
    QString getEmbeddingAIKey() const { return embeddingAIKey; }
    QString getEmbeddingAIModel() const { return embeddingAIModel; }

    bool getUseBuiltinChatAI() const { return useBuiltinChatAI; }
    bool getUseBuiltinEmbeddingAI() const { return useBuiltinEmbeddingAI; }
    QString getChatAIModelPath() const { return chatAIModelPath; }
    QString getChatAIHost() const { return chatAIHost; }
    QString getChatAIPort() const { return chatAIPort; }
    QString getChatAITemp() const { return chatAITemp; }
    QString getChatAIContextSize() const { return chatAIContextSize; }
    QString getChatAINgl() const { return chatAINgl; }
    QString getEmbeddingAIModelPath() const { return embeddingAIModelPath; }
    QString getEmbeddingAIHost() const { return embeddingAIHost; }
    QString getEmbeddingAIPort() const { return embeddingAIPort; }
    QString getEmbeddingAITemp() const { return embeddingAITemp; }
    QString getEmbeddingAIContextSize() const { return embeddingAIContextSize; }
    QString getEmbeddingAINgl() const { return embeddingAINgl; }

private:
    QString chatAIUrl;
    QString chatAIKey;
    QString chatAIModel;
    QString embeddingAIUrl;
    QString embeddingAIKey;
    QString embeddingAIModel;

    bool useBuiltinChatAI;
    bool useBuiltinEmbeddingAI;
    QString chatAIModelPath;
    QString chatAIHost;
    QString chatAIPort;
    QString chatAITemp;
    QString chatAIContextSize;
    QString chatAINgl;
    QString embeddingAIModelPath;
    QString embeddingAIHost;
    QString embeddingAIPort;
    QString embeddingAITemp;
    QString embeddingAIContextSize;
    QString embeddingAINgl;

    QNetworkAccessManager *networkManager;

    LlamaServerManager *m_llamaServerManager;

    bool performUrlTest(const QString &url, const QString &apiKey, const QString &model, bool isChat, QString *resultMessage);
};

#endif // AISERVICESETTINGS_H
